#!/bin/bash
#
# rebase a set of integration test patches
#
# Example:
#
# 1 - Extract kernel sources:
#
# % (rpm -ivh kernel-3.10.0-1127.el7.src.rpm; \
#    cd ~/rpmbuild/SPECS; \
#    rpmbuild --nodeps -bp kernel.spec)
#
#
# 2 - Rebase from previous release tests:
#
# % cd test/integration
# % SRCDIR="$HOME/rpmbuild/BUILD/kernel-3.10.0-1127.el7/linux-3.10.0-1127.el7.x86_64" \
#    ID=rhel VERSION_ID=7.8 ./rebase-patches rhel-7.7/*{.patch,.disabled}
# % cp rhel-7.7/*.test rhel-7.8/

OUTDIR=$(pwd)/${ID}-${VERSION_ID}
mkdir -p "$OUTDIR"

echo "* Making backup copy of kernel sources"
rm -rf "${SRCDIR}.orig"
cp -r "$SRCDIR" "${SRCDIR}.orig"

for P in "$@"; do

  echo
  echo "* Patch: $(basename "$P")"

  echo "** dry run..."
  if ! patch -d "$SRCDIR" --dry-run --quiet -p1 < "$P"; then
    echo "*** Skipping! ***" && continue
  fi

  echo "** patching..."
  patch -d "$SRCDIR" -p1 --no-backup-if-mismatch < "$P"

  echo "** generating new $(basename "$P")..."
  NEWP="$OUTDIR"/$(basename "$P")
  awk '/^Index|^diff|^patch/{exit} {print $LF}' "$P" > "$NEWP"
  diff -Nupr "$SRCDIR.orig" "${SRCDIR}" >> "$NEWP"
  sed -i "s#$SRCDIR#src#g" "$NEWP"

  echo "** reversing patch to restore tree..."
  patch -d "$SRCDIR" -p1 -R < "$NEWP"

done

echo "*** Removing backup copy of kernel sources"
rm -rf "${SRCDIR}.orig"

echo
echo "*** Done"
